//
//  FQDetailHeaderCollectionViewCell.swift
//  SwiftProject
//
//  Created by 董駸 on 2023/4/14.
//

import UIKit

class FQDetailHeaderCollectionViewCell: UICollectionViewCell, UITableViewDataSource, UITableViewDelegate {
    var model : FQDetailHeaderModel! {
        didSet {
            tableView.reloadData()
            titleLb.text = "XX产品备份"
            limitLb.text = "额度范围 10000-100000元"
            imageV.backgroundColor = .white
        }
    }

    let imageV = UIImageView()
    let titleLb = QMUILabel(font: .rSFont(16), textColor: .white)
    let limitLb = QMUILabel(font: .rSFont(15), textColor: .white)
    
    let tableView = UITableView(frame: .zero, style: .plain)
    
    var dataArray : [[String : Any]] = [["title" : "计息方式", "detail" : "等额本息"],
                                        ["title" : "借款期限", "detail" : "等额本息"],
                                        ["title" : "平均下款时间", "detail" : "等额本息"],
                                        ["title" : "综合通过率", "detail" : "等额本息"],
                                        ["title" : "借款利率", "detail" : "等额本息"],
                                        ["title" : "提前还款", "detail" : "等额本息"],
                                        ["title" : "前置费用", "detail" : "等额本息"]]
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = QMUICMI.backgroundColor
        
        let background = UIImageView(imageString: "detail_background")
        background.contentMode = .scaleAspectFill
        contentView.addSubview(background)
        
        background.snp.makeConstraints { make in
            make.left.right.top.equalToSuperview()
        }
        
        imageV.contentMode = .scaleAspectFill
        imageV.clipsToBounds = true
        imageV.layer.cornerRadius = 8
        contentView.addSubview(imageV)
        
        imageV.snp.makeConstraints { make in
            make.top.equalTo(NavigationContentTop + 10)
            make.centerX.equalToSuperview()
            make.size.equalTo(70)
        }
        
        contentView.addSubview(titleLb)
        titleLb.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(imageV.snp.bottom).offset(6)
        }
        
        contentView.addSubview(limitLb)
        limitLb.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(titleLb.snp.bottom).offset(12)
        }
        
        tableView.delegate = self
        tableView.dataSource = self
        tableView.rowHeight = 40
        tableView.register(FQDetailListTableViewCell.self, forCellReuseIdentifier: "cell")
        tableView.layer.cornerRadius = 8
        tableView.clipsToBounds = true
        tableView.backgroundColor = .white
        tableView.isUserInteractionEnabled = false
        contentView.addSubview(tableView)
        
        tableView.snp.makeConstraints { make in
            make.left.equalTo(20)
            make.right.equalTo(-20)
            make.top.equalTo(background.snp.bottom).offset(-40)
            make.height.equalTo(7 * 40 + 45)
        }
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        45
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view = UIView(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH - 40, height: 45))
        let button = QMUIButton(title: "机构详情", titleColor: .color51, font: .mSFont(16))
        button.setImage(UIImage(named: "detail_icon"), for: .normal)
        button.spacingBetweenImageAndTitle = 5
        button.imagePosition = .left
        button.isUserInteractionEnabled = false
        view.addSubview(button)
        
        button.snp.makeConstraints { make in
            make.left.equalTo(24)
            make.centerY.equalToSuperview()
        }
        return view
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        dataArray.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! FQDetailListTableViewCell
        cell.data = dataArray[indexPath.row]
        return cell
    }
    
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        cell.separatorInset = UIEdgeInsets(top: 0, left: 24, bottom: 0, right: 24)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
